package com.libs.xxf.pay_module.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alipay.sdk.app.PayTask;

import java.util.Map;

/**
 * TODO
 *
 * @author XXF
 *         Create Time : 2017/12/14 16:24
 */
public class PayUtils {

    /** 支付宝支付业务：入参app_id */
    public static final String APPID = "2016082600314465";

    /** 支付宝账户登录授权业务：入参pid值 */
    public static final String PID = "2088312555873221";
    /** 支付宝账户登录授权业务：入参target_id值 */
    public static final String TARGET_ID = "";

    /** 商户私钥，pkcs8格式 */
    /** 如下私钥，RSA2_PRIVATE 或者 RSA_PRIVATE 只需要填入一个 */
    /** 如果商户两个都设置了，优先使用 RSA2_PRIVATE */
    /** RSA2_PRIVATE 可以保证商户交易在更加安全的环境下进行，建议使用 RSA2_PRIVATE */
    /** 获取 RSA2_PRIVATE，建议使用支付宝提供的公私钥生成工具生成， */
    /** 工具地址：https://doc.open.alipay.com/docs/doc.htm?treeId=291&articleId=106097&docType=1 */
    public static final String RSA2_PRIVATE ="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCVFofO9bh8cG745TRJoN9eoDOCzOmTyb7j5LkqfMNTaME35czhrvjdB73pwti1EmGPV503XjVkP0lYawU4IcEN2yOV882QxrhS917g4F/2GlIgDZBbJpHzIK4UI2eNhhJr3/996svffi284i7ImYcCJKcILFGuQmTZhn/FMN6xzS3t7lmPRtDFj/x5tmPmCdvTfy313i+z3wqsqYZOsv2XYJruy9flpsUS4eeTpWg46nE1uDmQb0jDm9LwqVVBPRiFBvOiHXRPZuQOTME6/GGyv7qLloWuBnbWKHpGOqoADyLgtHY9NcM5Yt+3zEGB5rpyuRBfS09p03TuX2zw8+KhAgMBAAECggEASBKGQNSlBPNNkmErb9SnZxVHEZqB6IIFGCKAKTv7KR1hGuaqrwdpI2EUvCcHoDFsnGEcjaGrafT2Phougb4/l7UydNFRtQV8Ig+X/F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dkFnPawrYy+qfAD/6i/fyk+FJzJekDV7XozYKYsttpoog==";
    public static final String RSA_PRIVATE = "";

    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;


    /**
     * 支付宝支付业务
     *
     * @param v
     */
    public void payV2(View v, final Activity activity) {
        if (TextUtils.isEmpty(APPID) || (TextUtils.isEmpty(RSA2_PRIVATE) && TextUtils.isEmpty(RSA_PRIVATE))) {
            new AlertDialog.Builder(activity).setTitle("警告").setMessage("需要配置APPID | RSA_PRIVATE")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialoginterface, int i) {
                            //
                            activity.finish();
                        }
                    }).show();
            return;
        }

        /**
         * 这里只是为了方便直接向商户展示支付宝的整个支付流程；所以Demo中加签过程直接放在客户端完成；
         * 真实App里，privateKey等数据严禁放在客户端，加签过程务必要放在服务端完成；
         * 防止商户私密数据泄露，造成不必要的资金损失，及面临各种安全风险；
         *
         * orderInfo的获取必须来自服务端；
         */
        boolean rsa2 = (RSA2_PRIVATE.length() > 0);
        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);
        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);

        String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
        String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);
        final String orderInfo = orderParam + "&" + sign;

        Runnable payRunnable = new Runnable() {

            @Override
            public void run() {
                PayTask alipay = new PayTask(activity);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
//                mHandler.sendMessage(msg);
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }
}
